package com.ali.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.nio.charset.StandardCharsets;

/**
 * 自定义客户端处理器
 */
public class MyClientHandler extends ChannelInboundHandlerAdapter {
    /**
     * 通道激活
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        for (int i = 1; i < 4; i++) {
            String message = "hello，服务端，这是第" + i + "次的消息。";
            System.out.println("发送消息：" + message);
            //构建缓冲区
            ByteBuf byteBuf = Unpooled.copiedBuffer(message, StandardCharsets.UTF_8);
            //写入返回内容
            ctx.write(byteBuf);
            //刷新
            ctx.flush();
        }
    }
}
